<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2016/11/25
 * Time: 13:57
 * 支付记录
 */
require('../../../env.php');
header('Content-Type: text/html;charset=utf-8');
$start_date = $_GET['start_date'];
$end_date = $_GET['end_date'];
$user_name = $_GET['user_name'];
if ($start_date == '' || $end_date == '') {
    echo '开始时间或者结束时间都不能为空';
    exit;
}

$start_time = strtotime($start_date);
$end_time = strtotime($end_date) + (23 * 3600) + (59 * 60) + 59;

if ($start_time > $end_time) {
    echo '开始时间不能小于结束时间';
    exit;
}

if (($end_time - $start_time) > 30 * 24 * 3600) {
    echo '查询周期不能操作30天';
    exit;
}


function getCountBySearch($search)
{


    try {
        global $DATABASE_LIST;
        $dsn = 'mysql:dbname=' . $DATABASE_LIST[DB_ID]['db_name'] . ';host=' . $DATABASE_LIST[DB_ID]['server'] . ';port=' . $DATABASE_LIST[DB_ID]['port'];
        $user = $DATABASE_LIST[DB_ID]['username'];
        $password = $DATABASE_LIST[DB_ID]['password'];
        $dbh = new PDO($dsn, $user, $password);
        $dbh->query("set names utf8");
        $sql = "select count(*) as c from payment_log $search";
        $count = $dbh->query($sql)->fetch();
        return $count;
    } catch (PDOException $e) {
        echo $e->getMessage();
    }
}


function getAllPayLogs($search_str)
{
    try {
        global $DATABASE_LIST;
        $dsn = 'mysql:dbname=' . $DATABASE_LIST[DB_ID]['db_name'] . ';host=' . $DATABASE_LIST[DB_ID]['server'] . ';port=' . $DATABASE_LIST[DB_ID]['port'];
        $user = $DATABASE_LIST[DB_ID]['username'];
        $password = $DATABASE_LIST[DB_ID]['password'];
        $dbh = new PDO($dsn, $user, $password);
        $dbh->query("set names utf8");
        $all = $dbh->query("select * from payment_log $search_str")->fetchAll();
        return $all;
    } catch (PDOException $e) {
        echo $e->getMessage();
    }
}


$search_str = " WHERE createtime >= '$start_time' and createtime <= '$end_time'";
if ($user_name != '') {
    $search_str .= " AND user_name = '$user_name'";
} else {
    $search_str .= " AND 1=1";
}
//$count = getCountBySearch($search_str);
//$row_count = $count['c'];
//$page_size = PAGE_SIZE;
//$page_no = $page_no < 1 ? 1 : $page_no;
//
//$total_page = $row_count % $page_size == 0 ? $row_count / $page_size : ceil($row_count / $page_size);
//$total_page = $total_page < 1 ? 1 : $total_page;
//$page_no = $page_no > ($total_page) ? ($total_page) : $page_no;
//$start = ($page_no - 1) * $page_size;


$payLogs = getAllPayLogs($search_str);

foreach ($payLogs as $ord) {
    $new_order['OrderID'] = $ord['OrderID'];
    $new_order['MerchantID'] = $ord['MerchantID'];
    $new_order['TransNo'] = $ord['TransNo'];
    $new_order['Currency'] = $ord['Currency'];
    $new_order['Amount'] = $ord['Amount'];
    $new_order['Version'] = $ord['Version'];
    $new_order['BName'] = $ord['BName'];
    $new_order['BEmail'] = $ord['BEmail'];
    $new_order['BAddress'] = $ord['BAddress'];
    $new_order['BCity'] = $ord['BCity'];
//    $new_order['BState'] = $ord['BState'];
//    $new_order['BPostcode'] = $ord['BPostcode'];
//    $new_order['BCountry'] = $ord['BCountry'];
    $new_order['BPhone'] = $ord['BPhone'];
    $new_order['CardNumber'] = $ord['CardNumber'];
//    $new_order['CardMonth'] = $ord['CardMonth'];
//    $new_order['CardYear'] = $ord['CardYear'];
//    $new_order['CardCvv'] = $ord['CardCvv'];
    $new_order['URL'] = $ord['URL'];
    $new_order['IP'] = $ord['IP'];
    $new_order['UserAgent'] = $ord['UserAgent'];
    $new_order['AcceptLanguage'] = $ord['AcceptLanguage'];
    $new_order['Products'] = $ord['Products'];
    $new_order['RetCode'] = $ord['RetCode'];
    $new_order['RetStatus'] = $ord['RetStatus'];
    $new_order['RetMsg'] = $ord['RetMsg'];


    $new_order['createtime'] = date("Y-m-d H:i:s", $ord['createtime']);
    $real_logs[] = $new_order;
}
if (!$real_logs) {
    exit("无支付记录数据");
}

require_once dirname(__FILE__) . '/../include/lib/PHPExcelClasses/PHPExcel.php';
$fileName_s = "支付记录表";
$headArr_s = array(
    '订单号',
    '商户号',
    '交易号',
    '货币类型',
    '金额',
    '版本',
    '帐号用户',
    '账单邮件',
    '账单地址',
    '账单城市',
    '账单电话',
    '信用卡号',
    '交易网址',
    '客户IP',
    '浏览器代理',
    '浏览器语言',
    '产品信息',
    '支付返回码',
    '支付返回状态',
    '支付返回信息',
    '支付时间');
$data_source = $real_logs;

getExcel($fileName_s, $headArr_s, $data_source);

function getExcel($fileName, $headArr, $data)
{
    if (empty($data) || !is_array($data)) {
        die("data must be a array");
    }
    if (empty($fileName)) {
        exit;
    }
    $date = date("Y_m_d", time());
    $fileName .= "_{$date}.xlsx";

    //创建新的PHPExcel对象
    $objPHPExcel = new PHPExcel();
    $objProps = $objPHPExcel->getProperties();

    //设置表头
    $key = ord("A");
    foreach ($headArr as $v) {
        $colum = chr($key);
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v);
        $key += 1;
    }

    $column = 2;
    $objActSheet = $objPHPExcel->getActiveSheet();
    foreach ($data as $key => $rows) { //行写入
        $span = ord("A");
        foreach ($rows as $keyName => $value) {// 列写入
            $j = chr($span);
            $objActSheet->setCellValue($j . $column, $value);
            $span++;
        }
        $column++;
    }

    $fileName = iconv("utf-8", "gb2312", $fileName);
    //重命名表
    $objPHPExcel->getActiveSheet()->setTitle('Simple');
    //设置活动单指数到第一个表,所以Excel打开这是第一个表
    $objPHPExcel->setActiveSheetIndex(0);
    //将输出重定向到一个客户端web浏览器(Excel2007)
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header("Content-Disposition: attachment; filename=\"$fileName\"");
    header('Cache-Control: max-age=0');
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');

    $objWriter->save('php://output'); //文件通过浏览器下载

    exit;
}
